<template>
  <!-- openInstall配置 -->
  <div class="app-container">
    <div class="app-wrap">
      <search-table ref="table" :table-loading="listLoading" :table-data="dataList" @query="getDataList(true)" @reset="refresh">
        <template v-slot:row></template>
        <template v-slot:actions>
          <el-button v-permission="'openInstall.add'" type="primary" size="mini" icon="el-icon-plus" 
          @click="isShowDetails = true, checkedItem = null">新增</el-button>
          <el-button type="info" size="mini" icon="el-icon-refresh" @click="getDataList">刷新</el-button>
        </template>
        <template>
          <el-table-column align="center" type="index" width="50">
            <template slot-scope="scope">
              <el-radio v-model="scope.row.isChecked" :label="true" @change="handleCurrentChange(scope.row)">{{ '' }}</el-radio>
            </template>
          </el-table-column>
          <el-table-column prop="agent_id" label="代理ID" align="center" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-show="scope.row.agent_id">【{{scope.row.agent_id}}】{{ $public.channelLabel(scope.row.agent_id) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="channel_id" label="渠道ID" align="center" width="200" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{scope.row.channel_id}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="types" label="类型" align="center" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.types==1?'primary':'danger'">{{ scope.row.types==1?'安卓':'IOS'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_close" label="开启关闭" align="center" width="100">
            <template slot-scope="scope">
              <el-tag :type="scope.row.is_close==1?'info':'success'">{{ scope.row.is_close==1?'关闭':'开启'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_upper_package" label="是否是上架包" align="center" width="120">
            <template slot-scope="scope">
              <el-tag :type="scope.row.is_upper_package==1?'success':'info'">{{ scope.row.is_upper_package==1?'上架包':'普通包'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="app_key" label="AppKey" align="center" />
          <el-table-column prop="operation" label="操作" align="left" width="200">
            <template slot-scope="scope">
              <el-tooltip effect="dark" v-permission="'openInstall.edit'" content="编辑" placement="top-start">
                <el-button size="mini" type="warning" @click.stop="editItem(scope.row)" icon="el-icon-edit-outline" />
              </el-tooltip>
              <el-tooltip effect="dark" v-permission="'openInstall.delete'" content="删除" placement="top-start">
                <el-button size="mini" type="danger" @click="deleteItem(scope.row)" icon="el-icon-delete"/>
              </el-tooltip>
            </template>
          </el-table-column>
        </template>
        <template v-slot:footer>
          <pagination :total="currentTotal" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="getDataList" />
        </template>
      </search-table>
      <page-details :show="isShowDetails" :channelData="channelData" :data="checkedItem" @onHideDetails="onHideDetails"/>
    </div>
  </div>
</template>

<script>
import {
  throttle
} from '@/utils/index'
import {
  getOpenInstall,
  delOpenInstall
} from '@/api/config'
import PageDetails from './page-details.vue'
import SearchTable from '@/components/SearchTable'
export default {
  name: 'openInstall',
  components: {
    PageDetails,
    SearchTable
  },
  data () {
    return {
      channelData: this.$store.state.commonState.channelDataArray,
      listQuery: {
        page: 1,
        size: 50,
      },
      isShowDetails: false,
      checkedItem: null,
      listLoading: true,
      dataList: [],
      currentTotal: 0,
    }
  },
  created () {
    // this.getDataList();
  },
  activated(){
    this.channelData=this.$store.state.commonState.channelDataArray
    this.getDataList();
  },
  methods: {
    // openInstall列表
    async getDataList (query) {
      let that = this,params=that.listQuery;
      if(query && !query.page){
        params.page=1
        params.size=50
      }
      that.dataList = []
      that.checkedItem = null
      const response = await getOpenInstall(params)
      if (response.code == 1) {
        if(response.data.list){
          response.data.list.forEach(item => {
            item.isChecked = false
          })
          that.dataList = response.data.list
          that.currentTotal = response.data.total
        }
      }else{
        that.$message.error(response.msg)
      }
      that.listLoading = false
    },
    // 编辑/查看
    editItem (row) {
      this.isShowDetails=true
      this.checkedItem=row
      this.handleCurrentChange(row)
    },
    // 删除
    deleteItem (row) {
      const that = this
      that.handleCurrentChange(row)
      that.$confirm(`是否删除openInstall配置?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delOpenInstall({
          id: row.id
        }).then(res => {
          if (res.code == 1) {
            that.getDataList()
            that.$message({
              type: 'success',
              message: res.msg
            })
          }
        })
      }).catch((err) => {})
    },
    // 选择表格数据
    handleCurrentChange (val) {
      this.checkedItem = val
      this.dataList.map(item => {
        if (item.id === val.id) {
          item.isChecked = true
        } else {
          item.isChecked = false
        }
      })
    },
    onHideDetails (show) {
      this.isShowDetails = false
      if (show) {
        this.getDataList()
      }
    },
    // 刷新
    refresh: throttle(function () {
      this.listQuery = {
        page: 1,
        size: 50,
      }
      this.getDataList()
    }, 2000),
  }
}
</script>

<style lang="scss" scoped>
.pic-url {
  width: 90px;
  display: inline-block;
  object-fit: cover;
  border-radius: 4px;
}
</style>
